package com.ymttest.database.sqlwapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.FormatUtil;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.utils.EnvSetup;

public class YmtCouponBatchWapper {
    SqlManager manage;

    public YmtCouponBatchWapper() {
        manage = new SqlManager("ymt" + EnvSetup.getEnv());
    }

    // /**
    // * 查询最大的batchid
    // *
    // * @return
    // */
    // @SuppressWarnings("unchecked")
    // public Integer getMaxBatchId() {
    // ArrayList<YmtCouponBatch> list = new ArrayList<YmtCouponBatch>();
    // YmtCouponBatchExample data = new YmtCouponBatchExample();
    // data.setDistinct(false);
    // data.setOrderByClause("dAddTime desc");
    // list = (ArrayList<YmtCouponBatch>) manage
    // .getSelectResults(
    // "com.ymttest.database.mapping.YmtCouponBatchMapper.selectByExample",
    // data);
    // return list.get(0).getiBatchId();
    // }
    //
    // /**
    // * 生成优惠券批次号
    // *
    // * @param CouponnumPerUser
    // * 每个用户使用次数
    // * @param CouponSettingId
    // * 规则ID
    // * @param CouponTotalNum
    // * 优惠券总数
    // * @return
    // */
    // public String insertCouponBatchWapper(Integer CouponnumPerUser,
    // Integer CouponSettingId, Integer CouponTotalNum) {
    // YmtCouponBatch record = new YmtCouponBatch();
    // String daddtime = DateUtils.getBeforeOrNextDay(0);
    // record.setDaddtime(daddtime);
    // String dexpireddate = DateUtils.getBeforeOrNextDay(300);
    // record.setDexpireddate(dexpireddate);
    // // 每个用户领用次数
    // record.setIcouponnumperuser(CouponnumPerUser);
    // // 优惠券规则ID
    // record.setIcouponsettingid(CouponSettingId);
    // // 优惠券总数量
    // record.setIcoupontotalnum(CouponTotalNum);
    // record.setIoperatorid(303713);
    // // 发送方式 1-直接发放 2-链接发放
    // record.setIsendtype(2);
    // record.setIusage(0);
    // Calendar calendar = Calendar.getInstance();
    // int year = calendar.get(Calendar.YEAR) - 2000;
    // int month = calendar.get(Calendar.MONTH) + 1;
    // int day = calendar.get(Calendar.DATE);
    // String BatchCode = String.valueOf(year) + String.valueOf(month)
    // + String.valueOf(day) + (getMaxBatchId() + 1);
    // record.setSbatchcode(BatchCode);
    // int i = manage
    // .insert("com.ymttest.database.mapping.YmtCouponBatchMapper.insertSelective",
    // record);
    // manage.commit();
    // return BatchCode;
    // }

    public List<YmtCouponBatch> selectBysBatchCode(String sBatchCode) {
        List<YmtCouponBatch> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.selectBySBatchCode",
                        sBatchCode);
        return result;
    }
    
    public List<Map> selectBatchInfoBysBatchCode(String sBatchCode) {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.selectBatchInfoBysBatchCode",
                        sBatchCode);
        return result;
    }

    public List<Map> selectTop5BatchInfoBysBatchCode() {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.selectTop5BatchInfoBysBatchCode",
                        null);
        return result;
    }

    public YmtCouponBatch selectByPrimaryKey(Integer iBatchId) {
        YmtCouponBatch result = (YmtCouponBatch) manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.selectByPrimaryKey",
                        iBatchId).get(0);
        return result;
    }

    public List<Map> getCouponBatchByBatchCode(String sBatchCode) {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.getCouponBatchByBatchCode",
                        sBatchCode);
        return result;
    }

    public List<Map> getTop1CouponBatch() {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.getTop1CouponBatch", null);
        return result;
    }

    public List<Map> getTop1PlatformShortCodeCouponInfo() {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.getTop1PlatformShortCodeCouponBatch", null);
        return result;
    }

    public List<Map> getTop1PlatformLinkCouponInfo() {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.getTop1PlatformLinkCouponBatch", null);
        return result;
    }

    public List<Map> getTop1SellerCouponInfo() {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.getTop1SellerCouponBatch", null);
        return result;
    }

    public List<Map> getTop1SellerProductCouponInfo() {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.getTop1SellerProductCouponBatch", null);
        return result;
    }

    public List<Map> getAllSellerCouponList() {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.getAllSellerCouponList", null);
        return result;
    }

    public List<Map> getPlatformShortCodeCouponInfo() {
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.getAllSellerCouponList", null);
        return result;
    }

    public void setiCouponSettingId(int iBatchId, int iCouponSettingId) {
        Map m = new HashMap();
        m.put("iBatchId", iBatchId);
        m.put("iCouponSettingId", iCouponSettingId);
        manage.update(
                "com.ymttest.database.mapping.YmtCouponBatchMapper.setiCouponSettingId",
                m);
        manage.commit();
    }

    public void setiBatchCreateType(String sbatchCode, int iBatchCreateType) {
        Map m = new HashMap();
        m.put("sbatchCode", sbatchCode);
        m.put("iBatchCreateType", iBatchCreateType);
        manage
                .update(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.setiBatchCreateType",
                        m);
        manage.commit();
    }

    public void setbInvalidStatus(String sbatchCode, int bInvalidStatus) {
        Map m = new HashMap();
        m.put("sbatchCode", sbatchCode);
        m.put("iBatchCreateType", bInvalidStatus);
        manage
                .update(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.setbInvalidStatus",
                        m);
        manage.commit();
    }
    
    public void setiCouponTotalNum(String sbatchCode, int iCouponTotalNum) {
        Map m = new HashMap();
        m.put("sbatchCode", sbatchCode);
        m.put("iCouponTotalNum", iCouponTotalNum);
        manage
                .update(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.setiCouponTotalNum",
                        m);
        manage.commit();
    }

    public void setIsShowInPage(String sbatchCode, Boolean IsShowInPage) {
        Map m = new HashMap();
        m.put("sbatchCode", sbatchCode);
        m.put("IsShowInPage", IsShowInPage);
        manage
                .update(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.setIsShowInPage",
                        m);
        manage.commit();
    }

    public int getQueryValidCouponCount(int sellerId)
    {
    	List<Map> resultMap = manage
                 .getSelectResults(
                         "com.ymttest.database.mapping.YmtCouponBatchMapper.getQueryValidCouponCount", sellerId);
    	int result=Integer.parseInt(resultMap.get(0).get("count").toString());
         return result;
    }
    
    public void setAcquireStartTime(String batchCode, Date acquireStartTime) {
        Map m = new HashMap();
        m.put("batchCode", batchCode);
        m.put("acquireStartTime", acquireStartTime);
        manage
                .update(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.setAcquireStartTime",
                        m);
        manage.commit();
    }
    
    public void setAcquireEndTime(String batchCode, Date acquireEndTime) {
        Map m = new HashMap();
        m.put("batchCode", batchCode);
        m.put("acquireEndTime", acquireEndTime);
        manage
                .update(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.setAcquireEndTime",
                        m);
        manage.commit();
    }
    
    public List<Map> selectSellerReceiveBatches(int sellerId,Date effectiveStartTime,Date effectiveEndTime) {
    	 Map m = new HashMap();
         m.put("sellerId", sellerId);
         m.put("effectiveStartTime", effectiveStartTime);
         m.put("effectiveEndTime", effectiveEndTime);
         
        List<Map> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponBatchMapper.selectSellerReceiveBatches",
                        m);
        return result;
    }
    
	public List<Map> getSellerBatchesForApp(Integer pageSize, Integer sellerId, Integer batchStatus,
			Date lastBatchCreateTime) {
		Map m = new HashMap();
		m.put("pageSize", pageSize);
		m.put("sellerId", sellerId);
		m.put("batchStatus", batchStatus);
		m.put("lastBatchCreateTime", lastBatchCreateTime);

		List<Map> result = manage
				.getSelectResults("com.ymttest.database.mapping.YmtCouponBatchMapper.getSellerBatchesForApp", m);
		return result;
	}

    public int getSellerBatchCountInCurrentMonth(int sellerId,Date startTime, Date endTime)
    {
    	Map m = new HashMap();
        m.put("sellerId", sellerId);
        m.put("startTime", startTime);
        m.put("endTime", endTime);
        
    	int result = (int) manage
                 .getSelectResults(
                         "com.ymttest.database.mapping.YmtCouponBatchMapper.getSellerBatchCountInCurrentMonth", m).get(0);
    	       return result;
    }
    
    public List<Map> getUserWillExpireCouponCount(Integer pageSize, Integer days, Integer lastUserId)
    {
    	Map m = new HashMap();
        m.put("pageSize", pageSize);
        m.put("days", days);
        m.put("lastUserId", lastUserId);
        
        List<Map> result =  manage
                 .getSelectResults(
                         "com.ymttest.database.mapping.YmtCouponBatchMapper.getUserWillExpireCouponCount", m);
    	       
        return result;
    }
    
    public static void main(String[] args) {
        YmtCouponBatchWapper test = new YmtCouponBatchWapper();
        System.out.print(FormatUtil.beanToGSONString(test.selectBysBatchCode("1603305668")));
    }


}
